from django.core.urlresolvers import reverse
from django.shortcuts import *
from django.utils import feedgenerator
from django.template import RequestContext 
from django.utils.simplejson import *
from ytbase.helper import *
from ytbase.model.post import *
from ytcommunity.blog.setting import CATEGORY, BLOG_NUM_PER_PAGE, CONFIG, TAG_LIMIT
from ytcommunity.blog.helper.context import *
from ytbase.model.tag import *
from ytcommunity.blog.model import AUTH_MODEL, get_auth
import ytbase.view.post

def add(request, template):
    def filler(post):
        post.category = CATEGORY
        return post
    
    helpers = {}
    helpers[HELPER_GET_AUTH] = get_auth
    helpers[HELPER_FIILER] = filler
    helpers[HELPER_REDIRECTOR] = get_blog_url
    helpers[HELPER_PROCESSORS] = PROCESSORS
    return ytbase.view.post.add(request, template=template, 
        filler = filler,
        redirect_helper=get_blog_url, 
        processors=PROCESSORS,
        helpers = helpers )

def edit(request, id, template):
    return ytbase.view.post.edit(request, id=id, template=template, 
        redirect_helper=get_blog_url, 
        processors=PROCESSORS)
    
def promote(request, id):
    return ytbase.view.post.promote(request, id=id)    

def delete(request, id):
    return ytbase.view.post.delete(request, id=id)  

def list(request, template):
    filters = []
    filters.append(lambda q: q.filter('category =', CATEGORY))
    
    return ytbase.view.post.list(request, template=template,
        num_per_page = BLOG_NUM_PER_PAGE,
        filters = filters,
        processors = PROCESSORS)
    
def all(request, template):
    return list(request, template=template)
    
def my(request, template):
    filters = []
    filters.append(lambda q: q.filter('category =', CATEGORY))
    filters.append(lambda q: q.filter('author =', YTUser.get_current_user()))
    
    return ytbase.view.post.list(request, template=template,
        num_per_page = BLOG_NUM_PER_PAGE,
        filters = filters,
        processors = PROCESSORS)

def display(request, id, template):
    return ytbase.view.post.display(request, id=id, template=template,
        processors = PROCESSORS)    

def feed(request, title=CONFIG['site_title'], descr=CONFIG['site_description']):
    filters = []

    if request.GET.has_key('tag'):
        filters.append(lambda q: q.filter('tags =', request.GET['tag']))
        title = request.GET.get('tag', '') + ' - ' + title

    filters.append(lambda q: q.filter('category =', CATEGORY))
    filters.append(lambda q: q.order('-date'))
    posts, page = Paginator(request, YTPost, filters).get_page_objects()
    
    f = feedgenerator.Rss201rev2Feed(title=title, link=request.host_url, description=descr)
    for post in posts:
        f.add_item(
            title=post.title, 
            link=request.host_url+get_blog_url(post), 
            description=post.content,
            author_name=post.author.get_name(),
            pubdate=post.date)
        
    return HttpResponse(f.writeString('UTF-8') , mimetype="application/rss+xml")

@check_page_cache(True)
def tags(request):
    tags = YTTag.all().order('sort').order('-number').fetch(TAG_LIMIT)
    tags = [tag for tag in tags if tag.number > 0]
        
    def get_url(d):
        url = reverse('ytcommunity.blog.view.blog.all')
        name = urllib.quote(d.name.encode('utf-8'))
        return url + "?tag=" + name    

    from django.utils.html import escape
    def transfer(d):
        return {
            'name': escape(d.name),
            'url': get_url(d)
            }        
    
    context = [transfer(d) for d in tags]
    json = dumps(context)
    return HttpResponse(json, mimetype="application/json")

def get_blog_url(post):
    return reverse('ytcommunity.blog.view.blog.display', kwargs={'id':str(post.key().id())})
